package com.lagou.minicat.core;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.Servlet;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Context {

    private String path;
    private String docBase;
    private String appBase;
    private ContextClassLoader contextClassLoader;

    private List<Wrapper> wrappers = new ArrayList<>();

    public Context(String path, String docBase, String appBase) {
        this.path = path;
        this.docBase = docBase;
        this.appBase = appBase;
        ContextClassLoader contextClassLoader = new ContextClassLoader();
        contextClassLoader.setClassBase(appBase + docBase);
        this.contextClassLoader = contextClassLoader;
    }

    public void init() {
        try {
            // 读取web.xml
            String webConfigXmlPath = appBase + docBase + "/WEB-INF/web.xml";
            SAXReader saxReader = new SAXReader();
            Document webConfigDoc = saxReader.read(new File(webConfigXmlPath));
            loadWrappers(webConfigDoc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void loadWrappers(Document webConfigDoc) {
        try {
            Element rootElement = webConfigDoc.getRootElement();
            List<Element> selectNodes = rootElement.selectNodes("//servlet");
            for (int i = 0; i < selectNodes.size(); i++) {
                Element element =  selectNodes.get(i);
                Element servletNameElement = (Element) element.selectSingleNode("servlet-name");
                String servletName = servletNameElement.getStringValue();
                Element servletClassElement = (Element) element.selectSingleNode("servlet-class");
                String servletClass = servletClassElement.getStringValue();
                Element servletMapping = (Element) rootElement.selectSingleNode("/web-app/servlet-mapping[servlet-name='" + servletName + "']");
                String urlPattern = servletMapping.selectSingleNode("url-pattern").getStringValue();
                Class clazz = contextClassLoader.findClass(servletClass);
                Servlet servlet = (Servlet) clazz.newInstance();
                Wrapper wrapper = new Wrapper(urlPattern, servletClass, servlet);
                wrappers.add(wrapper);
                System.out.println(String.format("register wrapper, wrapper=%s", wrapper));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getDocBase() {
        return docBase;
    }

    public void setDocBase(String docBase) {
        this.docBase = docBase;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public List<Wrapper> getWrappers() {
        return wrappers;
    }

    public void setWrappers(List<Wrapper> wrappers) {
        this.wrappers = wrappers;
    }

    public String getAppBase() {
        return appBase;
    }

    public void setAppBase(String appBase) {
        this.appBase = appBase;
    }

    public ContextClassLoader getContextClassLoader() {
        return contextClassLoader;
    }

    public void setContextClassLoader(ContextClassLoader contextClassLoader) {
        this.contextClassLoader = contextClassLoader;
    }
}
